package com.say.activity;

import java.util.ArrayList;

import com.say.test.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class DialogsActivity extends Activity implements Runnable {
	private Button btn_diaNormal; 
	private Button btn_diaMulti;
	private Button btn_diaList;
	private Button btn_diaSinChos;
	private Button btn_diaMultiChos;
	private Button btn_diaProcess;
	private Button btn_diaReadProcess;
	private Button btn_diaCustom;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dialogs);
		getView();
		setListener();
	}
	
	private void getView()
	{
		btn_diaNormal=(Button)findViewById(R.id.btn_diaNormal);
		btn_diaMulti=(Button)findViewById(R.id.btn_diaMulti);
		btn_diaList=(Button)findViewById(R.id.btn_diaList);
		btn_diaSinChos=(Button)findViewById(R.id.btn_diaSigChos);
		btn_diaMultiChos=(Button)findViewById(R.id.btn_diaMultiChos);
		btn_diaProcess=(Button)findViewById(R.id.btn_diaProcess);
		btn_diaReadProcess=(Button)findViewById(R.id.btn_diaReadProcess);
		btn_diaCustom=(Button)findViewById(R.id.btn_diaCustom);
		
	}
	
	private void setListener()
	{
		btn_diaNormal.setOnClickListener(btnListener);
		btn_diaMulti.setOnClickListener(btnListener);
		btn_diaList.setOnClickListener(btnListener);
		btn_diaSinChos.setOnClickListener(btnListener);
		btn_diaMultiChos.setOnClickListener(btnListener);
		btn_diaProcess.setOnClickListener(btnListener);
		btn_diaReadProcess.setOnClickListener(btnListener);
		btn_diaCustom.setOnClickListener(btnListener);
	}
	
	private Button.OnClickListener btnListener= new Button.OnClickListener()
	{
		public void onClick(View v)
		{
			if(v instanceof Button)
			{
				int btnId=v.getId();
				switch(btnId)
				{
					case R.id.btn_diaNormal:
						showNormalDia();
						break;
					case R.id.btn_diaMulti:
						showMultiDia();
						break;
					case R.id.btn_diaList:
						showListDia();
						break;
					case R.id.btn_diaSigChos:
						showSinChosDia();
						break;
					case R.id.btn_diaMultiChos:
						showMultiChosDia();
						break;
					case R.id.btn_diaReadProcess:
						showReadProcess();
						break;
					case R.id.btn_diaProcess:
						showProcessDia();
						break;
					case R.id.btn_diaCustom:
						showCustomDia();
						break;
					default:
						break;
				}
			}
		}
	};

	/* 普通Dialog */
	private void showNormalDia()
	{
		//AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext());
		AlertDialog.Builder normalDia = new AlertDialog.Builder(DialogsActivity.this);
		normalDia.setIcon(R.drawable.ic_launcher);
		normalDia.setTitle("这里是标题");
		normalDia.setMessage("这里是message");
		
		normalDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				toast("确定");
			}
		});
		normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				toast("取消");
			}
		});
		normalDia.create().show();
	}
	
	/* 三个按钮的Dialog */
	private void showMultiDia()
	{
		AlertDialog.Builder multiDia=new AlertDialog.Builder(DialogsActivity.this);
		multiDia.setTitle("这里是标题");
		multiDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				toast("确定");
			}
		});
		multiDia.setNeutralButton("中间", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				toast("中间");
			}
		});
		multiDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				toast("取消");
			}
		});
		multiDia.create().show();
	}
	
	
	/* 列表Dialog，直接弹出条目内容，点击目标 选择 */
	private void showListDia()
	{
		final String[] mList={"条目1","条目2","条目3","条目4","条目5","条目6","条目7"};
		AlertDialog.Builder listDia=new AlertDialog.Builder(DialogsActivity.this);
		listDia.setTitle("这里是标题");
		listDia.setItems(mList, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				toast(mList[which]);
			}
		});
		listDia.create().show();
	}
	
	/* 单项选择 */
	int yourChose=-1;
	private void showSinChosDia()
	{
		final String[] mList={"条目1","条目2","条目3","条目4","条目5","条目6","条目7"};
		yourChose=-1;
		AlertDialog.Builder sinChosDia=new AlertDialog.Builder(DialogsActivity.this);
		sinChosDia.setTitle("这里是标题");
		sinChosDia.setSingleChoiceItems(mList, 0, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				yourChose=which;
			}
		});
		sinChosDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				if(yourChose!=-1)
				{
					toast(mList[yourChose]);
				}
			}
		});
		sinChosDia.create().show();
	}

	ArrayList<Integer> myChose= new ArrayList<Integer>();
	/* 多项选择 对话框  */
	private void showMultiChosDia() {
		final String[] mList = { "条目1", "条目2", "条目3", "条目4", "条目5", "条目6","条目7" };
		final boolean mChoseSts[] = { false, false, false, false, false, false, false };
		myChose.clear();
		AlertDialog.Builder multiChosDia = new AlertDialog.Builder(DialogsActivity.this);
		multiChosDia.setTitle("这里是标题");
		multiChosDia.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if(keyCode == event.KEYCODE_BACK){
					toast("按back");
					return true;
				}
				return false;
			}
		});
		multiChosDia.setMultiChoiceItems(mList, mChoseSts,
				new DialogInterface.OnMultiChoiceClickListener() {
					public void onClick(DialogInterface dialog, int which,boolean isChecked) {
						if (isChecked) {
							myChose.add(Integer.valueOf(which));
						} else {
							myChose.remove(Integer.valueOf(which));
						}
					}
				});
		multiChosDia.setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						int size = myChose.size();
						if(size == 0) return;
						StringBuilder sb = new StringBuilder();
						for (int i = 0; i < size; i++) {
							sb.append(mList[myChose.get(i)]);
							sb.append(',');
						}
						sb.deleteCharAt(sb.length()-1);
						toast(sb.toString());
					}
				});
		multiChosDia.create().show();
	}
	
	ProgressDialog mReadProcessDialog = null;
	public final static int MAX_READPROCESS = 100;
	/* 进度条对话框 */
	private void showReadProcess() {
		mReadProcessDialog = new ProgressDialog(DialogsActivity.this);
		mReadProcessDialog.setProgress(0);
		mReadProcessDialog.setTitle("这里是标题");
		mReadProcessDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		mReadProcessDialog.setMax(MAX_READPROCESS);
		//点击外部不关闭
		mReadProcessDialog.setCanceledOnTouchOutside(false);
		mReadProcessDialog.show();
		new Thread(this).start();
	}
	
	@Override
	public void run() {
		int Progress = 0;
		while (Progress < MAX_READPROCESS) {
			try {
				Thread.sleep(100);
				Progress++;
				mReadProcessDialog.incrementProgressBy(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		mReadProcessDialog.cancel();
	}
	
	/* 圆圈循环进度条 */
	private void showProcessDia() {
		ProgressDialog processDia = new ProgressDialog(DialogsActivity.this);
		processDia.setTitle("这里是标题");
		processDia.setMessage("这里是message");
		processDia.setIndeterminate(true);
		processDia.setCancelable(true);
		processDia.show();
	}
	
	/* 自定义对话框，可以放入一个布局 */
	private void showCustomDia()
	{
		AlertDialog.Builder customDia=new AlertDialog.Builder(DialogsActivity.this);
		View viewDia =
LayoutInflater.from(DialogsActivity.this).inflate(R.layout.activity_dialogs, null);
		customDia.setTitle("这里是标题");
		LinearLayout ll = (LinearLayout) viewDia;
		TextView tv = new TextView(DialogsActivity.this);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = android.view.Gravity.CENTER_HORIZONTAL;
		// 为这个view定制的布局参数
		ll.addView(tv, 0, params);
		customDia.setView(viewDia);
		
		tv.setText("text1");
		tv.setTextColor(Color.BLUE);
		
		customDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				toast("确定");
			}
		});
		customDia.create().show();
	}
	
	private void toast(String message) {
		Toast.makeText(DialogsActivity.this, message, Toast.LENGTH_SHORT).show();
	}
}
